package com.gouqi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.common.BaseResponse;
import com.gouqi.common.DeleteRequest;
import com.gouqi.common.ErrorCode;
import com.gouqi.common.ResultUtils;
import com.gouqi.exception.ThrowUtils;
import com.gouqi.model.dto.scoringResult.ScoringResultAddRequest;
import com.gouqi.model.dto.scoringResult.ScoringResultEditRequest;
import com.gouqi.model.dto.scoringResult.ScoringResultQueryRequest;
import com.gouqi.model.dto.scoringResult.ScoringResultUpdateRequest;
import com.gouqi.model.entity.ScoringResult;
import com.gouqi.model.vo.ScoringResultVO;
import com.gouqi.service.ScoringResultService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评分结果接口
 */
@RestController
@RequestMapping("/scoringResult")
@Slf4j
public class ScoringResultController {

    @Resource
    private ScoringResultService scoringResultService;

    // region 增删改查

    @ApiOperation("创建评分结果")
    @PostMapping("/add")
    public BaseResponse<Long> addScoringResult(@RequestBody ScoringResultAddRequest scoringResultAddRequest, HttpServletRequest request) {
        long scoringResultId = scoringResultService.addScoringResult(scoringResultAddRequest, request);
        return ResultUtils.success(scoringResultId);
    }

    @ApiOperation("删除评分结果")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteScoringResult(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        scoringResultService.deleteScoringResult(deleteRequest, request);
        return ResultUtils.success(true);
    }

    @ApiOperation("更新评分结果（仅管理员可用）")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateScoringResult(@RequestBody ScoringResultUpdateRequest scoringResultUpdateRequest) {
        scoringResultService.updateScoringResult(scoringResultUpdateRequest);
        return ResultUtils.success(true);
    }

    @ApiOperation("根据 id 获取评分结果（封装类）")
    @GetMapping("/get/vo")
    public BaseResponse<ScoringResultVO> getScoringResultVOById(long id, HttpServletRequest request) {
        ScoringResultVO scoringResultVO = scoringResultService.getScoringResult(id, request);
        return ResultUtils.success(scoringResultVO);
    }

    @ApiOperation("分页获取评分结果列表（仅管理员可用）")
    @PostMapping("/list/page")
    public BaseResponse<Page<ScoringResult>> listScoringResultByPage(@RequestBody ScoringResultQueryRequest scoringResultQueryRequest) {
        Page<ScoringResult> scoringResultPage = scoringResultService.getScoringResultListPage(scoringResultQueryRequest);
        return ResultUtils.success(scoringResultPage);
    }

    @ApiOperation("分页获取评分结果列表（封装类）")
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<ScoringResultVO>> listScoringResultVOByPage(@RequestBody ScoringResultQueryRequest scoringResultQueryRequest,
                                                                         HttpServletRequest request) {
        long current = scoringResultQueryRequest.getCurrent();
        long size = scoringResultQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<ScoringResult> scoringResultPage = scoringResultService.page(new Page<>(current, size),
                scoringResultService.getQueryWrapper(scoringResultQueryRequest));
        // 获取封装类
        return ResultUtils.success(scoringResultService.getScoringResultVOPage(scoringResultPage, request));
    }

    @ApiOperation("分页获取当前登录用户创建的评分结果列表")
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<ScoringResultVO>> listMyScoringResultVOByPage(@RequestBody ScoringResultQueryRequest scoringResultQueryRequest,
                                                                           HttpServletRequest request) {
        Page<ScoringResultVO> scoringResultVOPage = scoringResultService.getMyScoringResultVOPage(scoringResultQueryRequest, request);
        return ResultUtils.success(scoringResultVOPage);
    }


    @ApiOperation("编辑评分结果（给用户使用）")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editScoringResult(@RequestBody ScoringResultEditRequest scoringResultEditRequest, HttpServletRequest request) {
        scoringResultService.editScoringResult(scoringResultEditRequest, request);
        return ResultUtils.success(true);
    }

    // endregion
}
